﻿using ELab.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace ELab.Connector
{
    public class UDPSend
    {
        private static UDPSend _current = null;
        public static UDPSend Current
        {
            get
            {
                if (_current == null) _current = new UDPSend();
                return _current;
            }
        }
        private UdpClient client = new UdpClient();
        public UDPSend(string ipAddress = "127.0.0.1", int port = 11000)
        {
            this.Connect(ipAddress, port);
        }

        /// <summary>
        /// Connect to endpoint where server is listening
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <param name="port"></param>
        public void Connect(string ipAddress = "127.0.0.1", int port = 11000)
        {
            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ipAddress), 11000);
            client.Connect(ep);
        }

        /// <summary>
        /// Send message
        /// </summary>
        /// <param name="mess"></param>
        public void Send(EMessage mess)
        {
            if (mess.IsValidate) client.Send(mess.Data, mess.Data.Length);
            else
            {
                throw new Exception("Data is invalidate");
            }
        }
    }
}
